<?php
namespace app\admin\controller;
class Lunbo extends Common
{
    public function lst()
    {
        $lunboRes=db('lunbo')->order('id desc')->paginate(10);
        $count=db('lunbo')->count();
        $this->assign([
            'lunboRes'=>$lunboRes,
            'count'=>$count,
        ]);
        return view();
    }

    public function add()
    {
        if(request()->isPost()){
            $data=input('post.')['data'];
            unset($data['file']);
            $add=db('lunbo')->insert($data);
            if($add){
                return ['msg'=>'新增轮播成功','icon'=>1];
            }else{
                return ['msg'=>'新增轮播失败','icon'=>2];
            }
            return;
        }
        return view();
    }

    public function edit()
    {
        if(request()->isPost()){
            $data=input('post.')['data'];
            unset($data['file']);
            $save=db('lunbo')->update($data);
            if($save !== false){
                return ['msg'=>'编辑轮播成功','icon'=>1];
            }else{
                return ['msg'=>'编辑轮播失败','icon'=>2];
            }
            return;
        }
        $id=input('id');
        $lunbo=db('lunbo')->find($id);
        $this->assign([
            'lunbo'=>$lunbo
        ]);
        return view();
    }

    public function del()
    {
        $id=input('id');
        //删除轮播图片
        $img_url=db('lunbo')->field('img_url')->find($id);
        $Img=$img_url['img_url'];
        @unlink(LUNBOIMG.$Img);
        $del=db('lunbo')->delete($id);
        if($del){
            return ['msg'=>'删除轮播成功','icon'=>1];
        }else{
            return ['msg'=>'删除轮播失败','icon'=>2];
        }
    }

    //layui图片上传
    public function upload(){
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(LUNBOIMG);
        $data = array();  //定义一个返回的数组
        if($info){
            $reubfo['code']= 1;
            $reubfo['savename'] = $info->getSaveName(); 
        }else{
            // 上传失败获取错误信息
            $reubfo['code']= 0;
            $reubfo['err'] = $file->getError();
        } 
        return json_encode($reubfo);
    }

    //ajax栏目添加修改页面图片撤销功能 删除本地图片
    public function cancel(){
        $id=input('id');
        $imgurl=input('imgurl');
        $res=@unlink(LUNBOIMG.$imgurl);
        if($id){
            db('lunbo')->where('id',$id)->setField('img_url','');
        }
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }
    //ajax更新开启状态
    public function changeStatus()
    {
        if(request()->isAjax()){
            $id=input('id');
            $status=input('status');
            if($status == 1){
                db('lunbo')->where(['id'=>$id])->update(['status'=>0]);
                return ['msg'=>'已隐藏','icon'=>2];
            }else{
                db('lunbo')->where(['id'=>$id])->update(['status'=>1]);
                return ['msg'=>'已显示','icon'=>1];
            }
        }else{
            $this->error("非法操作！");
        }
    }
  	//栏目排序
    public function sort()
    {
        if(request()->isAjax()){
            $id=input('id');
            $sort=input('sort');
            $save=db('lunbo')->where(['id'=>$id])->update(['sort'=>$sort]);
            if($save !== false){
                return ['msg'=>'已排序','icon'=>1];
            }
        }

    }


}
